<?php
namespace Home\Controller;
use Common\Controller\CommonController;
use Org\Util\Wechat;
class MessageController extends CommonController {
	public $wx = null; 
	
	public function _initialize(){
		parent::_initialize();
		$this->wx = new Wechat(C('options'));
	}
	

	//客服列表
	public function count_list(){
		$sy_rbac_manager=M('rbac_manager');
		$count      = $sy_rbac_manager->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		$list=$sy_rbac_manager->alias('rm')->join('LEFT JOIN __KF__ as kf ON rm.openid = kf.openid')->join('LEFT JOIN __CUSTOMER__ AS c ON rm.openid=c.openid')->limit($Page->firstRow.','.$Page->listRows)->field('rm.*,kf.*,kf.id as kf_id,c.id as customer_id')->select();
		$arr=array('page'=>$show,'list'=>$list,'count'=>$count);
		$this->assign('list',$arr);
		$this->display('kefu');
	}

	//问题应答首页
	public function question(){

		$aptModel=M('apt');
		$customerModel=M('customer');
		$userMessageModel=M('user_message');
		$weObj = new \Org\Util\Wechat(C('options'));

		$openid=I('openid');
		$customer_id=I('customer_id');
		$user = $customerModel->where(['id'=>$customer_id])->find();
		$openid = $user['openid'];
		$user_id = $user['id'];
		$userMessage = $userMessageModel->where('from_uid="'.$user_id.'" or to_uid = "'.$user_id.'"')->order('create_time desc')->select();
		//print_R($user);die;
		if(!empty($userMessage)){
			foreach($userMessage as $kk=> $vv){
				$to_uid[] = $vv['to_uid'];
				$from_uid[] = $vv['from_uid'];
			}
			$newarray = array_merge($to_uid,$from_uid);
			$toidlist = array_unique($newarray);
			foreach($toidlist as  $kkk=> $vvv){
				if($vvv != $user_id){
					$touser = $customerModel->where('id="'.$vvv.'"')->find();
					$apt = $aptModel->where('id="'.$touser['plot'].'"')->find();
					$touser['plot'] = $apt['name'] ?$apt['name'] : '';
					if($touser){
						$touser['name'] = $touser['note_name'] ? $touser['note_name'] : $touser['name'];
					}
					$touserList[] = $touser;
				}
				
			}
			//同步所有用户头像
			foreach($touserList as $kkk){
				if(empty($kkk['headimgurl'])){
					if($userinfo = $weObj->getUserInfo($kkk['openid'])){
						$update['nickname'] = $this->filter_Emoji($userinfo['nickname']);
						$update['headimgurl'] = $userinfo['headimgurl'];
						$customerModel->where('id="'.$kkk['id'].'"')->save($update);
					}
				}
			}
		}

		//print_R($touserList);die;
		$this->assign('openid',$openid);
		$this->assign('from_uid',$user_id);
		$this->assign('list',$touserList);
		$this->display('userlist');
	}


	//messagelog
	public function messagelog(){
		$customerModel=M('customer');
		$userMessageModel=M('user_message');
		$openid=I('openid');
		// 客服id
		$user_id = $from_uid=I('from_uid');
		// 用户id
		$to_uid=I('to_uid');
		$userMessage = $userMessageModel->where('(from_uid="'.$from_uid.'" and to_uid = "'.$to_uid.'") or (from_uid="'.$to_uid.'" and to_uid = "'.$from_uid.'")')->order('create_time asc')->select();
		$weObj = new \Org\Util\Wechat(C('options'));
		foreach($userMessage as &$vv){
			$vv['from_user'] = $customerModel->where('id="'.$vv['from_uid'].'"')->find();
			$vv['to_user'] = $customerModel->where('id="'.$vv['to_uid'].'"')->find();
		}
		$this->assign('openid',$openid);
		$this->assign('from_uid',$user_id);
		$this->assign('list',$userMessage);
		$this->display('messagelog');
	}


	public function updateuserinfo(){
		$customerModel=M('customer');
		$touserList = $customerModel->select();
		$weObj = new \Org\Util\Wechat(C('options'));
		//同步所有用户头像
		foreach($touserList as $kkk){
			if(empty($kkk['headimgurl']) || empty($kkk['nickname'])){
				if($userinfo = $weObj->getUserInfo($kkk['openid'])){
					$update['nickname'] = $this->filter_Emoji($userinfo['nickname']);
					$update['headimgurl'] = $userinfo['headimgurl'];
					$customerModel->where('id="'.$kkk['id'].'"')->save($update);
				}
			}
			
		}
		echo "同步成功";
		die;
	}

	function filter_Emoji($str)
	{
		$str = preg_replace_callback(    //执行一个正则表达式搜索并且使用一个回调进行替换
				'/./u',
				function (array $match) {
					return strlen($match[0]) >= 4 ? '' : $match[0];
				},
				$str);
	 
		 return $str;
	 }
}